package mutsumimod.actions;

import com.megacrit.cardcrawl.actions.AbstractGameAction;
import com.megacrit.cardcrawl.actions.common.ApplyPowerAction;
import com.megacrit.cardcrawl.actions.common.ReducePowerAction;
import com.megacrit.cardcrawl.cards.AbstractCard;
import com.megacrit.cardcrawl.cards.CardGroup;
import com.megacrit.cardcrawl.core.AbstractCreature;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.powers.AbstractPower;
import mutsumimod.cards.BaseCard;
import mutsumimod.monitors.EatMonitor;
import mutsumimod.powers.BasePower;
import mutsumimod.powers.StandPower;
import mutsumimod.powers.BlindPower;

import java.util.ArrayList;
import java.util.Arrays;

public class TriggerStandAction extends AbstractGameAction {
    private final AbstractCreature target;
    private final int targetAmount;
    private final AbstractGameAction linkedAction;
    public boolean isPerfectStand;
    // 用于在不使用此行动时检查上次是否触发
    public static boolean latestTriggered = false;

    public TriggerStandAction(AbstractCreature target, int targetAmount, AbstractGameAction linkedAction,boolean isPerfectStand) {
        this.target = target;
        this.targetAmount = targetAmount;
        this.linkedAction = linkedAction;
        this.duration = Settings.ACTION_DUR_FAST;
        this.isPerfectStand = isPerfectStand;
    }


    public TriggerStandAction(AbstractCreature target, int targetAmount, AbstractGameAction linkedAction) {
        this(target, targetAmount, linkedAction, false);

    }

    public TriggerStandAction(int targetAmount, AbstractGameAction linkedAction) {
        this(AbstractDungeon.player, targetAmount, linkedAction);
    }

    @Override
    public void update() {
        if (this.isDone) {
            this.tickDuration();
            return;
        }
        int standAmount = 0;
        if(AbstractDungeon.player.hasPower(StandPower.POWER_ID)){
            standAmount = AbstractDungeon.player.getPower(StandPower.POWER_ID).amount;
        }
        if(!AbstractDungeon.player.hasPower(BlindPower.POWER_ID)){
            if(isPerfectStand) {
                if(standAmount != targetAmount) {
                    isDone = true;
                    return;
                }
            } else if ( standAmount < this.targetAmount) {
                this.addToTop(new ApplyPowerAction(target, target, new StandPower(target, 1), 1));
                this.isDone = true;
                this.tickDuration();
                return;
            }
        }

        TriggerStandAction.latestTriggered = true;
        EatMonitor.addStandAmountThisTurn(this.targetAmount);

        if(standAmount<targetAmount&&AbstractDungeon.player.hasPower(BlindPower.POWER_ID)){
            addToTop(new ReducePowerAction(AbstractDungeon.player,AbstractDungeon.player,BlindPower.POWER_ID,1));
        }

        for(AbstractPower p : AbstractDungeon.player.powers) {
            if(p instanceof BasePower){
                ((BasePower) p).onTriggerStand(standAmount,targetAmount);
            }
        }

        for(CardGroup group: new ArrayList<>(Arrays.asList(AbstractDungeon.player.drawPile,AbstractDungeon.player.hand,AbstractDungeon.player.discardPile))){
            for(AbstractCard c : group.group){
                if(c instanceof BaseCard){
                    ((BaseCard) c).onTriggerStand(targetAmount);
                }
            }
        }

        this.addToTop(this.linkedAction);
        this.isDone = true;
        this.tickDuration();

    }
}
